// import {ajax_tool, myDebounce, changObj} from './common.js';
import {sessionTime, verifyCode} from './request.js';

let top = document.querySelector('.top');
let middle = document.querySelector('.middle');
let img = document.querySelector('.img');
let btns = document.querySelectorAll(".btns button");
let screenshot = document.querySelector('.screenshot');
let btn = document.querySelector('.btn');
let section = document.querySelector('section');

// 获取参数和执行函数
(function parseData() {
    let obj = {};
    obj.token = sessionStorage.getItem('token');
    obj.day_time = sessionStorage.getItem('day_time');
    // 请求数据
    message(obj);
})();

// 请求某一天的场次和预约某天的场次的数据
async function message(obj) {
    // 请求场次
    let data = await sessionTime(obj);
    // 渲染姓名时间数据
    renderer(data);
    // 请求二维码数据并渲染
    reservationCode();
    console.log(data);
}

// 渲染姓名时间数据
function renderer(data) {
    // 名字
    let name = sessionStorage.getItem('name');
    // 日期
    let date = sessionStorage.getItem('day_time');
    // 时间
    let time = data.result.list[sessionStorage.getItem('show_id') - 1].show_name;
    top.firstElementChild.innerHTML = `${name}`;
    middle.firstElementChild.innerHTML = `欢迎您在${new Date(date).getMonth() + 1}月${new Date(date).getDate()}日${time}参观2021广州国际花卉艺术展暨中国插花花艺展。`;
    // console.log(date);
    // console.log(date);
    // console.log(time);
}

// 请求二维码数据并渲染
async function reservationCode() {
    let obj2 = {};
    obj2.token = sessionStorage.getItem('token');
    obj2.id = sessionStorage.getItem('id');
    // ajax请求二维码
    let data2 = await verifyCode(obj2);
    // 获取到二维码的链接
    let base64Img = data2.data.data;
    console.log(base64Img);
    // 渲染二维码照片上页面
    img.firstElementChild.src = base64Img;
}

// 点击返回首页按钮返回首页
btns[0].addEventListener('click', () => {
    location.href = "../index.html";
})

// 点击继续预约按钮返回预约页面
btns[1].addEventListener('click', () => {
    // let token = changObj(location.search).token;
    // // console.log(token);
    location.href = `session.html`;
})

// 点击预约记录按钮查看预约记录
btns[2].addEventListener('click', () => {
    location.href = `record.html`;
})

// 点击保存图片按钮下载图片
// 二维码图片加载完毕之后，自动东截图
img.firstElementChild.addEventListener('load', async function() {
    let canvasImg = await html2canvas(document.body);
    let dataURL = canvasImg.toDataURL();
    // console.log(dataURL);
    screenshot.firstElementChild.src = dataURL;
    btn.firstElementChild.firstElementChild.href = dataURL;
    btn.firstElementChild.firstElementChild.download = "sessionCode.png";
    // 点击下载
    let a = document.querySelector('#download');
    // 点击截图显示图片
    a.addEventListener('click', function() {
        screenshot.className = "screenshot show_screenshot";
    })
    // 鼠标滑动页面消失截图
    screenshot.addEventListener('click', function() {
        screenshot.className = "screenshot";
        console.log(1);
    })
} )

console.log(sessionStorage.getItem("phone"));